<?php

/*
 * +----------------------------------------------------------------------
 * 作 者：weilp
 * +----------------------------------------------------------------------
 * 创建日期：2016-08-27
 * +----------------------------------------------------------------------
 * 文件描述：测验内容>>选择题
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class BlankAction extends CommonAction
{

    public function get_item_edit($formid)
    {
        // 查询数据
        $model = new BlankModel();
        $item = new BlankItemModel();
        
        $data = $model->field("id,
			                       code,
								   formid,
								   question,
								   score,
								   number,
								   vnumber,
								   operator,
								   update_date")
            ->where("id = '" . $formid . "'")
            ->find();
        // 查询填空题问题
        $data['item_list'] = $item->field("id,
										   intro,
										   content,
										   seq,
										   score")
            ->where("selectid = '" . $data['id'] . "'")
            ->order("seq ASC")
            ->select();
        return $data;
    }

    /*
     * public function get_item_detail($roomid,$item_id, $userid) {
     *
     * if(!empty($roomid)) {
     * $model = new RoomCourseTestSelectModel();
     * $item = new RoomCourseTestSelectItemModel();
     * $answer = new RoomCourseTestSelectAnswerModel();
     * $where['oldid'] = $item_id;
     * $where['roomid'] = $roomid;
     * } else{
     * $model = new CourseTestSelectModel();
     * $item = new CourseTestSelectItemModel();
     * $answer = new CourseTestSelectAnswerModel();
     * $where['id'] = $item_id;
     * }
     * $data = $model->field("id,
     * testid,
     * question,
     * answer,
     * score,
     * type,
     * get_field_dict_name('course_test_select', 'type', type) type_name")
     * ->where($where)
     * ->find();
     *
     * //查询当前选择题选项
     * $data['item_list'] = $item->field("id,
     * intro,
     * is_correct,
     * seq")
     * ->where("selectid = '".$data['id']."'")
     * ->order("seq ASC")
     * ->select();
     *
     * //查询当前用户作答情况
     * $data['user_answer'] = $answer->field("answer,
     * score")
     * ->where("selectid = '".$data['id']."'
     * and operator = '".$userid."'")
     * ->find();
     * return $data;
     * }
     */
    /**
     * 题目添加页面
     */
    public function add()
    {
        $roomid = $this->_request('formid');
        $this->assign('item_info', $this->get_form_info($this->_request('formid'))); // 课程基本信息（头部显示）
        $this->display();
    }

    /**
     * 将题目添加至数据库
     */
    public function insert()
    {
        $formid = $this->_request('formid');
        $mode = $this->_request('mode');
        $select = new BlankModel();
        $item = new BlankItemModel();
        $select->startTrans(); // 启动事务
        if (false !== $select->create()) {
            $insertId = $select->add();
            if (false !== $insertId) {
                $data = R('Admin/QuestionTestEdit/add_item_info', array(
                    $formid,
                    'blank',
                    $insertId
                ));
                if (false !== $data) {
                    // 添加填空题问题
                    $select_item = $this->_request('select_item');
                    $score = $this->_request('score_option');
                    foreach ($select_item as $key => $value) {
                        if (empty($value)) {
                            continue;
                        }
                        $item_data['selectid'] = $insertId;
                        $item_data['intro'] = $value;
                        $item_data['score'] = $score[$key];
                        $item_data['seq'] = $key;
                        // 将选项数据添加至数据库
                        $data = $item->create($item_data, 1);
                        if (false !== $data) {
                            if (false === $item->add($data)) {
                                $select->rollback(); // 回滚事务
                                $this->error('填空题问题添加失败！');
                            }
                        } else {
                            $select->rollback(); // 回滚事务
                            $this->error('填空题问题添加失败！' . $item->getError());
                        }
                    }
                    // 执行成功后的操作
                    $select->commit(); // 提交事务
                    $this->success('题目添加成功！', __APP__ . '?g=Admin&m=QuestionTestEdit&a=index&formid=' . $formid . '&mode=' . $mode . '#blank_' . $insertId);
                } else {
                    $select->rollback(); // 回滚事务
                    $this->error('题目添加失败1！');
                }
            } else {
                $select->rollback(); // 回滚事务
                $this->error('题目添加失败2！');
            }
        } else {
            $select->rollback(); // 回滚事务
            $this->error('题目添加失败3！' . $select->getError());
        }
    }

    /**
     * 题目修改页面
     */
    public function edit()
    {
        // 查询当前选择题信息
        $model = new BlankModel();
        $item = new BlankItemModel();
        
        $data = $model->where("id = '" . $this->_request('questionid') . "'")
            ->find();
        $this->assign('data', $data);
        
        // 查询当前问题信息
        
        $item_list = $item->where("selectid = '" . $data['id'] . "'")
            ->order("seq ASC")
            ->select();
        $this->assign('item_list', $item_list);
        
        // 获得问卷基本信息
        $this->assign('course_info', $this->get_form_info($this->_request('formid')));
        // $this->assign('type_list', set_field_dict_list('question_test_select', 'type'));
        
        $this->display();
    }

    /**
     * 将题目信息更新至数据库
     */
    public function update()
    {
        $select = new BlankModel();
        $item = new BlankItemModel();
        
        $select->startTrans(); // 启动事务
        if (false !== $select->create()) {
            if (false !== $select->save()) {
                // 更新选择题选项
                $itemid = $this->_request('itemid');
                $select_item = $this->_request('select_item');
                $score = $this->_request('score_option');
                foreach ($itemid as $key => $value) {
                    if (empty($itemid[$key])) {
                        continue;
                    }
                    $item_data['id'] = $value;
                    $item_data['selectid'] = $this->_request('id');
                    $item_data['intro'] = $select_item[$key];
                    $item_data['score'] = $score[$key];
                    $item_data['seq'] = $key;
                    
                    if ($value == '-1') { // 添加新选项
                        unset($item_data['id']); // 销毁id
                        $data = $item->create($item_data, 1);
                        if (false !== $data) {
                            if (false === $item->add($data)) {
                                $select->rollback(); // 回滚事务
                                $this->error('填空题添加失败！');
                            }
                        } else {
                            $select->rollback(); // 回滚事务
                            $this->error('填空题更新失败！' . $item->getError());
                        }
                    } else { // 更新旧选项
                        $data = $item->create($item_data, 0);
                        if (false !== $data) {
                            if (false === $item->save($data)) {
                                $select->rollback(); // 回滚事务
                                $this->error('填空题更新失败！');
                            }
                        } else {
                            $select->rollback(); // 回滚事务
                            $this->error('填空题更新失败！' . $item->getError());
                        }
                    }
                }
                // 执行成功后的操作
                $select->commit(); // 提交事务
                
                $this->success('题目更新成功！', __APP__ . '?g=Admin&m=QuestionTestEdit&a=index&formid=' . $this->_request('formid') . '&mode=edit' . '#blank_' . $this->_request('id'));
            } else {
                $select->rollback(); // 回滚事务
                $this->error('题目更新失败！');
            }
        } else {
            $select->rollback(); // 回滚事务
            $this->error('题目更新失败！' . $select->getError());
        }
    }

    /**
     * 删除题目信息
     */
    public function delete()
    {
        $id = $this->_request('questionid');
        if (! empty($id)) {
            $model = new BlankModel();
            $item = new BlankItemModel();
            
            $model->startTrans(); // 启动事务
                                  
            // 首先删除选择题选项信息
            if (false !== $item->where("selectid = '" . $id . "'")->delete()) {
                // 测验中的item_info字段
                $data = $model->field('id, formid')
                    ->where("id = '" . $id . "'")
                    ->find();
                
                // 删除选择题基本信息
                if (false !== $model->where("id = '" . $id . "'")->delete()) {
                    // 删除测验中的item_info字段
                    $result = R('Admin/QuestionTestEdit/delete_item_info', array(
                        $data['formid'],
                        'blank',
                        $data['id']
                    ));
                    if ($result) {
                        $model->commit(); // 提交事务
                        $this->ajaxReturn(null, '删除成功！', 1);
                    } else {
                        $model->rollback(); // 回滚事务
                        $this->ajaxReturn(null, '删除失败！', 0);
                    }
                } else {
                    $model->rollback(); // 回滚事务
                    $this->ajaxReturn(null, '填空题信息删除失败！', 0);
                }
            } else {
                $model->rollback(); // 回滚事务
                $this->ajaxReturn(null, '填空题选项删除失败！', 0);
            }
        } else {
            $this->ajaxReturn(null, '未找到要删除的题目！', 0);
        }
    }

    /**
     * 编辑填空题时从数据库删除选项操作
     */
    public function delItem()
    {
        $model = new BlankItemModel();
        if (false !== $model->where("id = '" . $this->_request('id') . "'")
            ->delete()) {
            $this->ajaxReturn(null, '选项从数据库中删除成功！', 1);
        } else {
            $this->ajaxReturn(null, '选项从数据库中删除失败！', 0);
        }
    }
}
?>